/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.view.main.LeadSheet;
import org.jfonia.view.main.Project;
import org.jfonia.view.components.ITunesNavigationHeader;

/**
 *
 * @author Rik Bauwens
 */
public class NavigationBar extends ITunesNavigationHeader implements Observer
{
        public NavigationBar()
        {
                super();
                setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
                Project.getInstance().addCollectionObserver(this);
        }

        private void addButton(String text, String actionCommand)
        {
                NavigationButton button = new NavigationButton(text);
                button.setActionCommand(actionCommand);
                if(Project.getInstance().getCurrentLeadSheet().hashCode()
                        == Integer.parseInt(actionCommand))
                        button.setSelected(true);
                button.addActionListener(new ActionListener()
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                JButton button = (JButton) e.getSource();
                                for(Component component : getComponents())
                                {
                                        if(component instanceof JButton)
                                                ((JButton) component).setSelected(false);
                                }
                                button.setSelected(true);
                                int leadSheetId = Integer.parseInt(button.getActionCommand());
                                Project.getInstance().showLeadSheet(leadSheetId);
                        }
                });
                add(button);                
        }

        public void onNotify(Object source)
        {
                for(Component component : getComponents())
                {
                        if(!component.isVisible())
                                remove(component);
                        component.setVisible(false);
                }

                for(LeadSheet leadSheet : Project.getInstance())
                {
                        addButton(Project.getInstance().getHeader(leadSheet),
                        Integer.toString(leadSheet.hashCode()));
                }
                //Ensure that all added components are directly drawn onto the component
                revalidate();
                requestFocus();
        }
}
